package com.gfxy.modules.base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.base.service.ITeacherBaseInfoService;
import com.gfxy.modules.base.service.ITeacherBaseInfoVoService;
import com.gfxy.modules.base.vo.TeacherBaseInfoVo;
import com.gfxy.modules.system.entity.SysUser;
import com.gfxy.modules.system.entity.SysUserDepartTeaching;
import com.gfxy.modules.system.service.ISysUserDepartTeachingService;
import com.gfxy.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/gfxy/userInfo")
public class UserInfoController {

    @Autowired
    ITeacherBaseInfoVoService teacherBaseInfoVoService;



    @Autowired
    private ITeacherBaseInfoService teacherBaseInfoService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<TeacherBaseInfoVo>> queryPageList(TeacherBaseInfoVo user, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<TeacherBaseInfoVo>> result = new Result<IPage<TeacherBaseInfoVo>>();
        QueryWrapper<TeacherBaseInfoVo> queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap());

        Page<TeacherBaseInfoVo> page = new Page<TeacherBaseInfoVo>(pageNo, pageSize);
        IPage<TeacherBaseInfoVo> pageList = teacherBaseInfoVoService.page(page, queryWrapper);

        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<TeacherBaseInfoVo> add(@RequestBody JSONObject jsonObject) {

        TeacherBaseInfoVo teacherBaseInfoVo = JSON.parseObject(jsonObject.toJSONString(), TeacherBaseInfoVo.class);

        teacherBaseInfoService.save(teacherBaseInfoVo);

        return Result.OK(teacherBaseInfoVo);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Result<TeacherBaseInfoVo> edit(@RequestBody JSONObject jsonObject) {

        TeacherBaseInfoVo teacherBaseInfoVo = JSON.parseObject(jsonObject.toJSONString(), TeacherBaseInfoVo.class);

        teacherBaseInfoService.edit(teacherBaseInfoVo);

        return Result.OK(teacherBaseInfoVo);
    }


    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public Result<?> resetPassword(@RequestBody JSONObject jsonObject) {

        String teacherId = jsonObject.getString("teacherId");
        this.teacherBaseInfoService.resetPassword(teacherId);

        return Result.ok("密码重置成功!");
    }

}
